package com.pay.game.central.web.service;

import com.github.pagehelper.Page;
import com.pay.game.central.utils.DateTimeUtil;
import com.pay.game.central.web.bean.PtScore;
import com.pay.game.central.web.bean.Settlement;
import com.pay.game.central.web.bean.Store;
import com.pay.game.central.web.mapper.PtScoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PtScoreService {

    @Autowired
    PtScoreMapper scoreMapper;

    public void insertItem(PtScore ptScore) {
        scoreMapper.insertItem(ptScore);
    }

    public Page<Map<String, Object>> list(String storeId, String storeName, String username) {
        return scoreMapper.list(storeId, storeName, username);
    }

    /**
     * 获取用户
     * allScore         所有店铺跑总限额
     * successScore     所有店铺跑分总额
     * runScore         当前开启店铺总限额
         * runSuccessScore  当前开启店铺跑分总额
     *
     * @param userId
     * @return
     */
    public Map<String, Integer> scoreInfo(String userId) {
        List<Settlement> settlements = scoreMapper.getSettlementScore(userId, DateTimeUtil.getFormatCurrentDate());
        List<Store> stores = scoreMapper.getStoreScore(userId);
        List<String> runStoreIds = stores.stream().filter(Store::isLoop).map(Store::getId).collect(Collectors.toList());
        int allScore = 0;
        int runScore = 0;
        int allSuccessScore = 0;
        int runSuccessScore = 0;
        // 累加今天已经结算的数据
        for (Settlement item: settlements) {
            allScore += item.getScore();
            allSuccessScore += item.getRun_score();
            if (runStoreIds.contains(item.getStore_id())) {
                runScore += item.getScore();
                runSuccessScore += item.getRun_score();
            }
        }
        // 累加当前跑分的数据
        for (Store item: stores) {
            allScore += item.getScore();
            allSuccessScore += item.getUsed_score();
            if (runStoreIds.contains(item.getId())) {
                runScore += item.getScore();
                runSuccessScore += item.getUsed_score();
            }
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("allScore", allScore);
        map.put("runScore", runScore);
        map.put("allSuccessScore", allSuccessScore);
        map.put("runSuccessScore", runSuccessScore);
        return map;
    }

}